<template>
  <div class="app-container">
    <el-form :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      class="form-search"
      label-width="80px">

      <el-form-item label="	章节名称"
        prop="chapterName">
        <el-input v-model="queryParams.chapterName"
          placeholder="请输入章节名称"
          clearable
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery">搜 索</el-button>
        <el-button icon="el-icon-refresh"
          size="mini"
          @click="resetQuery">重 置</el-button>
      </el-form-item>
    </el-form>
    <div class="section-box">
      <el-row :gutter="10"
        class="mb8">
        <el-col :span="1.5">
          <el-button type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning"
            plain
            icon="el-icon-close"
            size="mini"
            @click="handleClose">关闭</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading"
        :data="dataList">
        <el-table-column label="章节名称"
          align="center"
          prop="chapterName"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column label="排序"
          align="center"
          prop="sort"
          sortable
          :show-overflow-tooltip="true"
          min-width="100" />
        <el-table-column label="数据状态"
          align="center"
          prop="dataStatus"
          :show-overflow-tooltip="true"
          min-width="120">
          <template slot-scope="scope">
            <!-- <el-switch v-model="scope.row.dataStatus"
              active-value="1"
              inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch> -->
            <span>{{scope.row.dataStatus == '1'? '有效': '无效'}}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="内容"
          align="center"
          prop="content"
          :show-overflow-tooltip="true"
          min-width="160" /> -->
        <el-table-column label="创建人"
          align="center"
          prop="createBy"
          min-width="140"
          show-overflow-tooltip />
        <el-table-column label="创建时间"
          align="center"
          prop="createTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新人"
          align="center"
          prop="updateBy"
          min-width="140"
          show-overflow-tooltip />
        <el-table-column label="更新时间"
          align="center"
          prop="updateTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          min-width="140">
          <template slot-scope="scope">
            <el-button size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body>
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">

        <el-form-item label="章节名称"
          prop="chapterName">
          <el-input v-model.trim="form.chapterName"
            maxlength="50"
            placeholder="请输入章节名称">
          </el-input>
        </el-form-item>

        <el-form-item label="排序"
          prop="sort">
          <el-input v-model.trim="form.sort"
            maxlength="10"
            @input="form.sort = onInput(form.sort)"
            placeholder="请输入排序">
          </el-input>
        </el-form-item>
        <el-form-item label="数据状态"
          prop="dataStatus">
          <el-select v-model="form.dataStatus"
            placeholder="请选择数据状态"
            clearable
            style="width: 100%">
            <el-option key="1"
              label="有效"
              value="1" />
            <el-option key="0"
              label="无效"
              value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="内容"
          prop="content"
          style="width: 100%;">
          <el-input type="textarea"
            v-model="form.content"
            style="width: 100%;"
            :rows="4"
            maxlength="2000"
            placeholder="请输入内容">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getChapterList,
  chapterDetail,
  addChapter,
  updateChapter,
  delChapter
} from "@/api/resource/chapter.js";
import { onInput } from '@/utils/util.js'

export default {
  name: "chapter",
  data () {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookId: null,
        chapterName: null
      },
      // 数据列表
      dataList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created () {
    this.queryParams.bookId = this.$route.params && this.$route.params.bookId;
    this.getList();
  },
  methods: {
    onInput,
    /** 查询列表 */
    getList () {
      this.loading = true;
      getChapterList(this.queryParams).then(
        (response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 返回按钮
    handleClose () {
      const obj = { path: '/resource/books' };
      this.$tab.closeOpenPage(obj);
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        bookId: this.queryParams.bookId,
        chapterName: null,
        sort: null,
        dataStatus: null,
        acontentuth: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = '新增章节';
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const chapterId = row.chapterId;
      chapterDetail(chapterId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = '修改章节';
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.chapterId != undefined) {
            updateChapter(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addChapter(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const chapterId = row.chapterId;
      this.$modal
        // .confirm('是否确认删除Rate id编号为"' + rateIds + '"的数据项？')
        .confirm(`是否确认删除章节为 ${row.chapterName} 的数据项？`)
        .then(function () {
          return delChapter(chapterId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => { });
    }
  },
};
</script>
